package tchk.xyz.lingyuntutorminiprogram.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import tchk.xyz.lingyuntutorminiprogram.entity.TeacherSchedule;
import tchk.xyz.lingyuntutorminiprogram.service.ITeacherScheduleService;
import tchk.xyz.lingyuntutorminiprogram.service.IUserService;
import tchk.xyz.lingyuntutorminiprogram.utils.Result;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class TeacherController {

    @Autowired
    private ITeacherScheduleService scheduleService;

    // [新增] 注入 UserService，用于根据 openid 查询用户
    @Autowired
    private IUserService userService;

    /**
     * [核心修正] 只保留这一个用于更新日程的 POST 方法
     *
     * @param schedules      要保存的日程列表
     * @param authentication Spring Security 注入的当前用户信息
     */
@PostMapping("/schedule")
public Result<Void> updateSchedule(@RequestBody List<TeacherSchedule> schedules, Authentication authentication) {
    System.out.println("收到日程更新请求，数据量: " + (schedules != null ? schedules.size() : 0));

    // 调试信息
    if (authentication == null) {
        System.out.println("Authentication is null - 用户未认证");
        return Result.error("用户未认证");
    }

    try {
        String openid = authentication.getName();
        // 根据 openid 查询到 userId
        Long userId = userService.getOneByOpenid(openid).getId();

        System.out.println("更新用户ID " + userId + " 的日程");

        // 打印接收到的数据用于调试
        if (schedules != null) {
            for (TeacherSchedule schedule : schedules) {
                System.out.println("接收到的日程: date=" + schedule.getScheduleDate() +
                                 ", startTime=" + schedule.getStartTime() +
                                 ", endTime=" + schedule.getEndTime());
            }
        }

        scheduleService.updateTeacherSchedule(userId, schedules);

        System.out.println("日程更新成功");
        return Result.success();
    } catch (Exception e) {
        System.out.println("日程更新失败: " + e.getMessage());
        e.printStackTrace();
        return Result.error("日程更新失败: " + e.getMessage());
    }
}

    /*
     * [核心修正] 删除或注释掉所有其他与 /schedule 路径冲突的旧方法
     *
    @GetMapping("/schedule")
    public Result<Object> getSchedule() {
        // 此方法已移至 PublicController
        return Result.success("获取日程逻辑待实现");
    }

    @DeleteMapping("/schedule/{id}")
    public Result<Void> deleteSchedule(@PathVariable Long id) {
        // TODO: 删除一条可授课时间
        return Result.success();
    }
    */


    // --- 保留其他不冲突的 TODO 方法 ---

    @PostMapping("/apply")
    public Result<Void> applyToBeTeacher(@RequestParam("files") MultipartFile[] files) {
        // TODO: 实现教师认证申请逻辑
        return Result.success();
    }

    @PutMapping("/info")
    public Result<Void> updateTeacherInfo(@RequestBody Object teacherInfoDTO) {
        // TODO: 更新教师个人简介、课时费等信息
        return Result.success();
    }
}